menuList[3].onclick();

// 获取数据并渲染
current=1;
pageSize=10;
let result;
let searchContent = -1;
let flagData=1;//判断当前分页数据是那些类型 1、所有数据 2、搜索结果
async function fun(flagOptions){
    let checkAll = document.querySelector("#checkAll");
    checkAll.checked = false;
    dels=[];
    if(flagData==1){
        result = await getData(current,pageSize);
        counts = result.data.totalCounts;
        console.log(counts);
        pages = result.data.totalPage;
        pageFun({
            current:current,
            pages:pages,
            counts:counts,
            flagOptions:flagOptions,
            pageSize:pageSize
        })
    }else if(flagData==2){
        searchByUsername();
    }
   
}
fun(0);
function getData(current,pageSize){
    return new Promise((resolve, reject) => {
        axios({
            url:'/admin/store/findAllShops',
            method:'GET',
            params:{
                page:current,
                size:pageSize
                }
            })
            .then(data => {
                decorateFun(data)
                console.log(data.data);
                resolve(data.data);
            })
            .catch(err => {
                console.log(err);
            })
    });
   
}

// 批量删除
function openDeleteStoresFun(){
    console.log(dels);
    if(dels.length==0){
        explain({
            code:3,
            msg:"请选择需要删除的店铺",
            time:3000
        })
    }else{
        pointFun({
            code:4,
            msg:"是否确认删除选中的店铺"
        })
    }
}
function deleteStoresFun(){
    axios({
        url:'/admin/store/deleteSuchShops',
        method:'GET',
        params:{
            ids:dels
        }
    })
    .then(data => {
        console.log(data);
        if(data.data.code==20000){
            closeFunStore();
            fun(0);
            explain({
                code:1,
                msg:"删除成功",
                time:3000
            })
        }
    })
    .catch(err => {
        console.log(err);
    })
}
// 删除单个店铺
let shopid;
let shopname;
function openDeleteStoreFun(e){
    e=e || window.event;
    var target=e.target || e.srcElement;
    shopid=target.getAttribute('name');
    shopname=target.getAttribute('shopName');
    pointFun({
        code:3,
        msg:"是否删除该店铺"
    })
    console.warn(target.getAttribute('name'));
    console.warn(target.getAttribute('shopName'));
}
function deleteStoreFun(){
    axios({
        url:'/admin/store/deleteAShop',
        method:'DELETE',
        params:{
            userId:shopid,
            shopName:shopname
        }
    })
    .then(data => {
        console.log(data);
        if(data.data.code==20000){
            closeFunStore();
            fun(0);
            explain({
                code:1,
                msg:"删除成功",
                time:3000
            })
        }
    })
    .catch(err => {
        console.log(err);
    })
}
// 搜索通过店铺名
let searchInput = cl("searchInput");
function searchByStorename(){
    if(searchContent != searchInput.value){
        page=1;
        pageSize=10;
        current=1;
    } 
    console.warn(searchContent==searchInput.value);
    axios({
        url: '/admin/store/findSuchShops',
        method: 'GET',
        params:{
            shopName:searchInput.value,
            page: current,
            size: pageSize,
            }
        })
        .then(data => {
            if(data.data.code==20001){
                explain({
                    code:2,
                    msg:"抱歉未找到",
                    time:3000
                })
            }else{
                searchContent = searchInput.value
                flagData=2;
                result = data.data.data;
                counts = result.totalCounts;
                pages = result.totalPage
                console.log(result);
                decorateFunSearch(data)
                pageFun({
                    current:current,
                    pages:result.totalPage,
                    counts:result.totalCounts,
                    pageSize:result.size
                })
            }
        })
        .catch(err => {
            console.log(err);
        })
}

// 渲染页面
function decorateFun(data){
    let tbodyMenu = cl("tbodyMenu");
    let dataNums = cl("dataNums");
    let pageMessage = cl("pageMessage");
    let end;
    if(current==1){
        end=data.data.data.list.length;
    }else{
        end =parseInt(current-1)*parseInt(data.data.data.size)+parseInt(data.data.data.list.length);
    }
    dataNums.innerHTML = data.data.data.list.length;
    pageMessage.innerHTML = `第${(current-1)*data.data.data.size+1}到${end}条记录，共${data.data.data.totalCounts}条`;
    tbodyMenu.innerHTML ="";
    let i=0;
    for(let key of data.data.data.list){
        tbodyMenu.innerHTML += `
        <tr>
            <td><input type="checkbox" class="checkbox" name="${key.shopId}" onclick="checkboxFun()" target="${i}"  ></td>
            <td>${i+1}</td>
            <td  name="${key.shopId}">${key.shopName}</td>
            <td>${key.introduction}</td>
            <td>${key.time== null?'无':key.time}</td>
            <td>${key.setting=='1'?'通过':'未通过'}</td>
            <td>
                <a href="/admin/store/storeComment?shopId=${key.shopId}">
                    <button class="btn commentBtn"><i name="${key.userId}" shopName="${key.shopName}"  class="iconfont xiangqing-"></i></button>
                </a>
                <button class="btn delBtn"><i name="${key.userId}" shopName="${key.shopName}" onclick="openDeleteStoreFun()" class="iconfont shanchu"></i></button>
            </td>
        </tr>
            `
            i++
        }
}
function decorateFunSearch(data){
    let tbodyMenu = cl("tbodyMenu");
    let dataNums = cl("dataNums");
    let pageMessage = cl("pageMessage");
    let end;
    console.log(data.data.data);
    if(current==1){
        end=data.data.data.length;
    }else{
        end =parseInt(current-1)*parseInt(data.data.data.size)+parseInt(data.data.data.list.length);
    }
    dataNums.innerHTML = data.data.data.list.length;
    pageMessage.innerHTML = `第${(current-1)*data.data.data.size+1}到${end}条记录，共${data.data.data.totalCounts}条`;
    tbodyMenu.innerHTML ="";
    let i=0;
    for(let key of data.data.data.list){
        tbodyMenu.innerHTML += `
        <tr>
            <td><input type="checkbox" class="checkbox" name="${key.shopId}" onclick="checkboxFun()" target="${i}"  ></td>
            <td>${i+1}</td>
            <td  name="${key.shopId}">${key.shopName}</td>
            <td>${key.introduction}</td>
            <td>${key.time== null?'无':key.time}</td>
            <td>${key.setting=='1'?'通过':'未通过'}</td>
            <td>
                <a href="/admin/store/storeComment?shopId=${key.shopId}">
                    <button class="btn commentBtn"><i name="${key.userId}" shopName="${key.shopName}"  class="iconfont xiangqing-"></i></button>
                </a>
                <button class="btn delBtn"><i name="${key.userId}" shopName="${key.shopName}" onclick="openDeleteStoreFun()" class="iconfont shanchu"></i></button>
            </td>
        </tr>
            `
            i++
        }
}

// 输入框回车事件
function inputEnterFun(){
    console.log("a");
    if(event.keyCode == 13){
        searchByStorename()
    }
}

// 页面刷新
function reloadFun(){
    location.reload();
}

// 选择每页展示数量
function chooseSizeFun(){
    let selectNums = document.getElementById("selectNums");
    console.log(selectNums.children[1]);
    console.warn(selectNums.value);
    current = 1;
    pageSize =selectNums.value;
    fun(1);
    for(let i=0;i<selectNums.children.length;i++) {
        if(selectNums.children.length[i]==selectNums.value){
            selectNums.children.length[i].select="select";
        }
    }
}
// 关闭函数
function closeFunStore(){
    alertWindow = document.getElementsByClassName("alertWindow")[0];
    let covers = document.getElementsByClassName("covers")[0];
    covers.style.display = "none";
    alertWindow.style.display = "none";
}
